/**
 * 
 */
package org.mspring.mlog.web.module.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.PostCatalog;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.PostCatalogService;
import org.mspring.mlog.service.security.UserService;
import org.mspring.mlog.support.log.Log;
import org.mspring.mlog.web.query.PostCatalogQueryCriterion;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.query.QueryCriterion;
import org.mspring.platform.utils.SessionUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Gao Youbo
 * @since 2012-7-18
 * @Description
 * @TODO
 */
@Widget
@RequestMapping("/admin/post/catalog")
public class Admin_PostCatalogWidget extends Admin_AbstractWidget {
    @Autowired
    private PostCatalogService postCatalogService;
    @Autowired
    private UserService userService;

    /**
     * 列表
     * 
     * @param page
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping({ "/list" })
    @Log
    public String list(@ModelAttribute Page<PostCatalog> postCatalogPage, @ModelAttribute PostCatalog postCatalog, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (postCatalogPage == null) {
            postCatalogPage = new Page<PostCatalog>();
        }
        QueryCriterion queryCriterion = new PostCatalogQueryCriterion(queryParams);
        postCatalogPage = postCatalogService.listPage(queryCriterion, postCatalogPage);
        model.addAttribute("postCatalogPage", postCatalogPage);
        return "/admin/post/catalog/listPostCatalog";
    }

    /**
     * 创建分类页面
     * 
     * @param postCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/create")
    public String create(@ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/post/catalog/createPostCatalog";
    }

    /**
     * 创建分类
     * 
     * @param postCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/create/save")
    @Log
    public String create_save(@ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (postCatalog.getCreateTime() == null) {
            postCatalog.setCreateTime(new Date());
        }
        if (postCatalog.getUser() == null || postCatalog.getUser().getId() == null) {
            postCatalog.setUser(SecurityUtils.getCurrentUser(request));
        }
        postCatalogService.save(postCatalog);
        return "redirect:/admin/post/catalog/list";
    }

    @RequestMapping("/userAutocomplete")
    @ResponseBody
    public ResponseEntity userAutocomplete(@RequestParam(required = false) String name, HttpServletRequest request, HttpServletResponse response) {
        ResponseEntity rsp = new ResponseEntity();
        try {
            List<User> users = userService.listUserLeftLikeName(name);
            rsp.setSuccess(true);
            rsp.put("users", users);
            return rsp;
        } catch (Exception e) {
            // TODO: handle exception
            rsp.setSuccess(false);
            return rsp;
        }
    }

    /**
     * 修改页面
     * 
     * @param postCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String eidt(@ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (postCatalog == null || postCatalog.getId() == null) {
            Long id = SessionUtils.getAttrAsLong(request, "PostCatalogWidget_edit_id");
            if (id != null) {
                postCatalog = new PostCatalog(id);
            }
        }
        SessionUtils.setAttr(request, "PostCatalogWidget_edit_id", postCatalog.getId());

        if (postCatalog == null || postCatalog.getId() == null) {
            return prompt(model, "请先选择要修改的分类");
        }
        postCatalog = postCatalogService.get(postCatalog.getId());
        model.addAttribute("postCatalog", postCatalog);
        return "/admin/post/catalog/editPostCatalog";
    }

    /**
     * 执行修改操作
     * 
     * @param postCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        postCatalog.setModifyTime(new Date());
        postCatalogService.update(postCatalog);
        return "redirect:/admin/post/catalog/list";
    }

    @RequestMapping("/ctrl")
    @Log
    public String ctrl(@ModelAttribute Page<PostCatalog> postCatalogPage, @ModelAttribute PostCatalog postCatalog, @RequestParam Map queryParams, @RequestParam(required = false) Long[] ids, @RequestParam(required = false) Long[] deleteIds, @RequestParam(required = false) Long[] orders, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (deleteIds != null && deleteIds.length > 0) {
            postCatalogService.remove(deleteIds);
        }
        if (orders != null && orders.length > 0) {
            postCatalogService.setPostCatalogOrders(ids, orders);
        }
        return list(postCatalogPage, postCatalog, queryParams, request, response, model);
    }

}
